<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Shop;
use DB;

class ShopController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $shops = Shop::paginate(2);
        $count = Shop::count();
        return view('admin.shop_index')->with('shops',$shops)->with('count',$count);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop_add');
    }

    /**
     * Store a newly created resource in storage.
     *商店添加
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {  
         $img = $request->file("img");
        if($request->hasFile("img")){
            //确认上传的文件是否成功
            if($img->isValid()){
                $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $name = time().rand(1000,9999).".".$ext;
                $img->move("./upload/",$name);  
                $url =  "./upload/".$name;
                $shop = new Shop();
                $shop->shop_logo = $url;
                $shop->shop_name = $request->shop_name;
                $shop->store_name = $request->store_name;
                $shop->shop_phone = $request->shop_phone;
                $shop->shop_pwd = md5($request->shop_pwd);
                $shop->shop_email = $request->shop_email;
                $shop->shop_description = $request->shop_description;
                $res = $shop->save();
                return redirect('admin/shop');    
            }
        }
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $shops = Shop::find($id);
        return view('admin.shop_edit')->with('shops',$shops);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        Shop::where('id', $id)
            ->update($request->except('_token','_method'));
        return redirect('/admin/shop');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *状态改变
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function status(Request $request){
        $id = $request->id;
        $status = $request->status;
        return Shop::where('id',$id)->update(['shop_status'=>!$status]);
        
    }

    /**
     * Remove the specified resource from storage.
     *删除商家
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    public function del(Request $request){
        $id = $request->id;
        return Shop::where('id',$id)->delete();
    }

}
